<%-- 
    Document   : field_add_edit
    Created on : 05.01.2013, 20:40:50
    Author     : Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
--%>

<%@page import="mygang.controller.AbstractController"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add/Edit Field</title>
        <script type="text/javascript">
            var isEdit = ${isEdit};
            var elementSelect, entitySelect, columnSelect;
            
            function isAttribute(){
                return document.getElementById('attribute').wrapper.isChecked();
            }
            
            function updateFieldsState(){
                var isAttr = isAttribute();
                $('#column')[0].wrapper.setEnabled(!isAttr);
                if(!isAttr) getColumnsList();
            }
            
            $(document).ready(function(){
                uic.simpleform({
                    id: 'field',
                    showTitle: false,
                    target: $('div.add_edit'),
                    validate: {
                        ignore: ":disabled"
                    }
                }).add(uic.control.input({
                    id: 'title',
                    type: 'text',
                    placeholder: 'Title',   
                    value: '${obj.title}',
                    title: 'Title',
                    rules: {
                        required: true,
                        messages: {
                            required: "Please enter a name"
                        }
                    }
                })).add(elementSelect = uic.control.dropdown({
                    id: 'template',
                    title: 'Template',
                    disabled: isEdit,
                    rules: {
                        required: true,
                        messages: {
                            required: "Please select field template"
                        }
                    }
                })).add(uic.control.checkbox({
                    id: 'active',
                    checked: '${obj.active}',
                    title: 'Is Active'
                })).add(entitySelect = uic.control.dropdown({
                    id: 'entity',
                    title: 'Entity',
                    onchange: function(){
                        getColumnsList();
                    },
                    rules: {
                        required: true,
                        messages: {
                            required: "Please select a entity"
                        }
                    }
                })).add(uic.control.checkbox({
                    id: 'attribute',
                    checked: '${obj.attribute}',
                    title: 'Is Attribute',
                    onclick: function(e, self){
                        updateFieldsState();
                    }
                })).add(columnSelect = uic.control.dropdown({
                    id: 'column',
                    title: 'Column',
                    rules: {
                        required: true,
                        messages: {
                            required: "Please select a column"
                        }
                    }
                })).addButton(uic.element.button({
                    onclick: function(){
                        model.submit({
                            id: '${obj.id}',
                            title: $('#title').val(),
                            active: $('#active').is(':checked'),
                            attribute: $('#attribute').is(':checked'),
                            templateId : $('#template option:selected').val(),
                            entityId: $('#entity option:selected').val(),
                            column: $('#column option:selected').val()
                        }, isEdit);
                    },
                    id: 'submit',
                    title: 'Ok',
                    primary: true
                })).addButton(dialogManager.getCloseButton()).initValidation();
                
                getElementsList();
                getEntitiesList();
                getColumnsList();
                updateFieldsState();
            });
            
            function getElementsList(){
                $.ajax({
                    url: 'getTemplates',
                    type: "POST",
                    async: false,
                    success: function(list) {
                        elementSelect.setUnpreparedList({
                            list: list,
                            selectedValue: '${obj.template.id}',
                            needEmptyOption: !isEdit
                        });
                    }
                });
            }
            
            function getColumnsList(){
                var id = $('#entity option:selected').val();
                if(!id) {
                    columnSelect.clearList();
                    return;
                }
                $.ajax({
                    url: dialogManager.getBaseUrl()+"<%=AbstractController.MANAGE_ENTITY%>/getEntityColumns",
                    data: {
                        id: $('#entity option:selected').val()
                    },
                    type: "POST",
                    async: false,
                    success: function(list) {
                        columnSelect.setUnpreparedList({
                            list: list,
                            selectedValue: '${obj.columnName}',
                            needEmptyOption: true
                        });
                    }
                });
            }
            
            function getEntitiesList(){
                $.ajax({
                    url: dialogManager.getBaseUrl()+"<%=AbstractController.MANAGE_ENTITY%>/jAll",
                    type: "POST",
                    async: false,
                    success: function(list) {
                        entitySelect.setUnpreparedList({
                            list: list,
                            selectedValue: '${obj.entity.id}',
                            needEmptyOption: true
                        });
                    }
                });
            }
        </script>
    </head>
    <body>
        <div class="add_edit"></div>
    </body>
</html>
